﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Com.Dto;
using FwProject.Configuration;
using FwProject.Controllers;
using FwProject.EventAdvertising.Dto;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Configuration;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 商城广告
    /// </summary>
    public class AdvertisingController : FwProjectControllerBase
    {
        private readonly IShopAdLocationAppService _shopAdLocationAppService;
        private readonly IShopAdAppService _shopAdAppService;
        private readonly IConfigurationRoot _appConfiguration;

        public AdvertisingController(IShopAdLocationAppService shopAdLocationAppService, IHostingEnvironment env, IShopAdAppService shopAdAppService)
        {
            _shopAdLocationAppService = shopAdLocationAppService;
            _shopAdAppService = shopAdAppService;
            _appConfiguration = env.GetAppConfiguration();
        }
        #region 广告列表
        /// <summary>
        /// 广告列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(Guid? id)
        {
            var id2 = id ?? new Guid();
            return View(id2);
        }

        /// <summary>
        /// 添加广告信息
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Create()
        {
            //广告位置
            var spaces = await _shopAdLocationAppService.GetListAsync();
            ViewBag.Spaces = spaces.Select(o => new SelectListItem() { Text = o.AdLocationTitle, Value = o.Id.ToString() });
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Create(ShopAdInput input)
        {
            var id = await _shopAdAppService.CreateShopAdLocation(input);
            return Json(id);
        }

        /// <summary>
        /// 修改广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            ViewBag.ImageUrl = _appConfiguration["ImageUrl:url"].ToString();
            //广告位置
            var spaces = await _shopAdLocationAppService.GetListAsync();
            ViewBag.Spaces = spaces.Select(o => new SelectListItem() { Text = o.AdLocationTitle, Value = o.Id.ToString() });

            var entity = await _shopAdAppService.GetEnterDtoAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopAdInput input)
        {
            await _shopAdAppService.EditShopAdLocation(id, input);
            return Json(id);
        }

        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Delete(Guid id)
        {
            await _shopAdAppService.DeleteAdLocation(id);
            return Json(id);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetAdvertisingData(int page, int limit, string keyword, Guid spaceid)
        {
            var data = await _shopAdAppService.GetShopAdLayerDataAsync(page, limit, keyword, spaceid);
            return Json(data);
        }

        /// <summary>
        /// 广告开关
        /// </summary>
        /// <param name="id"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        public async Task<IActionResult> SwitchActive(Guid id, bool active)
        {
            await _shopAdAppService.SwitchActive(id, active);
            return Json(id);
        }

        /// <summary>
        /// 上传广告图片
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpLoadImg()
        {
            string FileServicePath = _appConfiguration["FileServicePath:TempAdvertising"].ToString();
            return Json(base.UploadFile(FileServicePath));
        }
        #endregion


        #region 广告位
        public IActionResult Space()
        {
            return View();
        }
        public IActionResult CreateSpace()
        {
            return View();
        }
        public async Task<IActionResult> EditSpace(Guid id)
        {
            ViewBag.Id = id;
            var entity = await _shopAdLocationAppService.GetEnterDtoAsync(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<IActionResult> EditSpace(Guid id, ShopAdLocationInput input)
        {
            await _shopAdLocationAppService.EditShopAdLocation(id, input);
            return Json(id);
        }

        [HttpPost]
        public async Task<IActionResult> GetByIdWidthAndHeight(Guid id)
        {
             var model= await _shopAdLocationAppService.GetEnterDtoAsync(id);
            return Json(model);
        }


        [HttpPost]
        public async Task<IActionResult> CreateSpace(ShopAdLocationInput input)
        {
            var id = await _shopAdLocationAppService.CreateShopAdLocation(input);
            return Json(id);
        }

        public async Task<IActionResult> DeleteSpace(Guid id)
        {
            await _shopAdLocationAppService.DeleteAdLocation(id);
            return Json(id);
        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetAdvertisingSpaceData(int page, int limit, string keyword)
        {
            var data = await _shopAdLocationAppService.GetShopAdLocationLayerDataAsync(page, limit, keyword);
            return Json(data);
        }
        #endregion

    }
}